package com.legna.xyq.alert.service;

import cn.hutool.http.HttpUtil;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;


@Service
public class HeartBeatService {

    @Autowired
    private EmailService emailService;

    /**
     * 是否为客户端
     */
    private boolean isClient = true;

    private boolean isAlive = true;

    private boolean isPlay = false;

    /**
     * 1、定时发送心跳  5秒一次
     */
    @Scheduled(cron = "0/5 * * * * ? ")
    public void sendHeartBeat(){
        if (isClient){
            //客户端才定时发送
            try {
                String result = HttpUtil.get("http://111.229.195.69:8725/xyq/heartbeat");
                System.out.println(result);
            }catch (Exception e1){
                try {
                    //请求出现异常，先判断是否有网
                    InetAddress address = InetAddress.getByName("www.baidu.com");
                    System.out.println("心跳接口请求失败，但网络正常！");
                    emailService.sendEmail("15067388403@163.com","15067388403@163.com","腾讯服务器服务异常！","心跳接口请求失败，但客户端网络正常！");
                } catch (Exception e2) {
                    System.out.println("心跳接口请求失败，客户端网络异常，尝试重新连接网络！");
                    try {
                        // 创建Runtime对象
                        Runtime runtime = Runtime.getRuntime();

                        // 执行cmd命令
                        Process process = runtime.exec("cmd /c rasdial 宽带连接 057379457860 121358");

                        // 读取命令执行结果
                        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
                        String line;
                        while ((line = reader.readLine()) != null) {
                            System.out.println(line);
                        }

                        // 关闭流
                        reader.close();

                        // 等待命令执行完成
                        process.waitFor();

                    } catch (Exception e3) {
                        e3.printStackTrace();
                        System.out.println("网络也连接异常，毁灭吧！");

                        if (isPlay){
                            //音频循环播放中
                            return;
                        }

                        File audioFile = new File("ai.mp3");

                        try {
                            Clip clip = AudioSystem.getClip();
                            AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);
                            clip.open(audioStream);

                            // 循环播放
                            clip.loop(Clip.LOOP_CONTINUOUSLY);

                            clip.start();

                            // 等待音频播放完成
                            Thread.sleep(clip.getMicrosecondLength() / 1000);

                            clip.close();
                            audioStream.close();

                            isPlay = true;

                        } catch (Exception e4) {
                            e4.printStackTrace();
                            System.out.println("没声音？去死吧！");
                        }
                    }
                }
            }
        }
    }



    /**
     * 2、定时将存货改为false  1分钟一次
     */
    @Scheduled(cron = "0 0/1 * * * ? ")
    public void offline(){
        if (!isClient){
            //服务端才需要修改这个状态
            isAlive = false;
        }
    }


    /**
     * 3、定时判断存货状态，不存活则发送邮件   1分钟一次
     */
    @Scheduled(cron = "0 0/1 * * * ? ")
    public void judgeAlive() throws InterruptedException {
        //服务端才需要定时判断离线
        if (!isClient && !isAlive){
            Thread.sleep(10000L);
            if (!isClient && !isAlive){
                emailService.sendEmail("15067388403@163.com","15067388403@163.com","电脑离线","你的电脑也断网或关机");
            }
        }
    }

    public void heartbeat() {
        //客户端不会来请求，就算修改了这个状态也无所谓
        isAlive = true;
    }
}
